/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Departmentteacher;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.TeacherService;
import me.zhengjie.modules.system.domain.vo.TeacherQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.utils.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author tianjiaqi
* @date 2024-04-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "教师管理")
@RequestMapping("/api/teacher")
public class TeacherController {

    private final TeacherService teacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('teacher:list')")
    public void exportTeacher(HttpServletResponse response, TeacherQueryCriteria criteria) throws IOException {
        teacherService.download(teacherService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询教师")
    @ApiOperation("查询教师")
    public ResponseEntity<PageResult<Teacher>> queryTeacher(TeacherQueryCriteria criteria, Page<Object> page){

        return new ResponseEntity<>(teacherService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/getSchoolTeacher")
    @Log("查询教师")
    @ApiOperation("查询教师")
//    @PreAuthorize("@el.check('teacher:list')")
    public ResponseEntity<List<User>> queryTeacher(TeacherQueryCriteria criteria){
        return new ResponseEntity<>(teacherService.getSchoolTeacher(criteria),HttpStatus.OK);
    }

    @GetMapping("/getTeacherListByIds")
    @Log("查询教师")
    @ApiOperation("查询教师")
//    @PreAuthorize("@el.check('teacher:list')")
    public ResponseEntity<List<User>> getTeacherListByIds(Integer[] ids){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("userId",ids);
        return new ResponseEntity<>(teacherService.list(queryWrapper),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增教师")
    @ApiOperation("新增教师")
//    @PreAuthorize("@el.check('teacher:add')")
    public R<Object> createTeacher(@Validated @RequestBody Teacher resources){
        Integer status = teacherService.create(resources);

        if(status == -1){
            return R.error("邮箱重复");
        }
        if(status == -3){
            return R.error("用户名已存在");
        }
        if(status == -4){
            return R.error("手机号重复");
        }
        if(status == -5){
            return R.error("工号重复");
        }

        return R.success("新增成功");
    }

    @PutMapping
    @Log("修改教师")
    @ApiOperation("修改教师")
//    @PreAuthorize("@el.check('teacher:edit')")
    public R<Object> updateTeacher(@Validated @RequestBody Teacher resources) throws Exception {

        Integer status =teacherService.update(resources);


        if(status == -1){
            return R.error("邮箱重复");
        }
        if(status == -3){
            return R.error("用户名已存在");
        }
        if(status == -4){
            return R.error("手机号重复");
        }
        if(status == -5){
            return R.error("工号重复");
        }

        return R.success("修改成功");
    }

    @DeleteMapping
    @Log("删除教师")
    @ApiOperation("删除教师")
//    @PreAuthorize("@el.check('teacher:del')")
    public ResponseEntity<Object> deleteTeacher(@RequestBody List<Integer> ids) {
        teacherService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}